package com.wingice.system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wingice.common.page.EntityPageBean;
import com.wingice.common.web.ErrorCodeInfo;
import com.wingice.system.entity.SysRole;
import com.wingice.system.exception.ResultBean;
import com.wingice.system.service.SysRoleService;
import com.wingice.system.validator.sys.role.SysRoleValidator;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 表基础信息 前端控制器
 * </p>
 *
 * @author 胡昊
 * @since 2020-07-23
 */
@RestController
@RequestMapping("/sysRole")
@Tag(name = "角色管理")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    /**
     * @param sysRole 角色信息
     * @description 新增角色
     * @author 胡昊
     * @email huhao9277@gmail.com
     * @date 2020/7/25 16:09:48
     */
    @PreAuthorize("@knifeSecurity.hasPermission('sysRole:insert')")
    @PostMapping("/insert")
    @Operation(summary = "新增角色")
    public ResultBean<Boolean> insert(@RequestBody SysRole sysRole) {
        //参数校验
        SysRoleValidator.insertValidate(sysRole);
        return ResultBean.restResult(sysRoleService.insertSysRole(sysRole), ErrorCodeInfo.CREATED);
    }

    /**
     * @param idList 待删除角色id列表
     * @description 批量删除角色
     * @author 胡昊
     * @email huhao9277@gmail.com
     * @date 2020/7/25 16:15:39
     */
    @PreAuthorize("@knifeSecurity.hasPermission('sysRole:deleteBatch')")
    @DeleteMapping("/deleteBatch")
    @Operation(summary = "批量删除角色")
    public ResultBean<Boolean> deleteBatch(@RequestBody List<String> idList) {
        //参数校验
        SysRoleValidator.deleteValidate(idList);
        return ResultBean.restResult(sysRoleService.deleteBatch(idList), ErrorCodeInfo.NO_CONTENT);
    }

    /**
     * @param sysRole 角色信息
     * @description 更新角色
     * @author 胡昊
     * @email huhao9277@gmail.com
     * @date 2020/7/25 16:23:22
     */
    @PreAuthorize("@knifeSecurity.hasPermission('sysRole:update')")
    @PatchMapping("/update")
    @Operation(summary = "更新角色")
    public ResultBean<Boolean> update(@RequestBody SysRole sysRole) {
        //参数校验
        SysRoleValidator.updateValidate(sysRole);
        return ResultBean.restResult(sysRoleService.updateSysRole(sysRole), ErrorCodeInfo.OK);
    }

    /**
     * @param entityPageBean 查询条件
     * @description 分页查询角色
     * @author 胡昊
     * @email huhao9277@gmail.com
     * @date 2020/7/25 16:28:38
     */
    @PreAuthorize("@knifeSecurity.hasPermission('sysRole:page')")
    @PostMapping("/page")
    @Operation(summary = "分页查询角色")
    public ResultBean<Page<SysRole>> page(@RequestBody EntityPageBean<SysRole> entityPageBean) {
        return ResultBean.restResult(sysRoleService.pageSysRole(entityPageBean), ErrorCodeInfo.OK);
    }
}

